<?php
	class Picture
	{
		public function convert_image($image, $theight, $twidth, $location, $temp_loc, $file_name)
		{
			if($image['type'] == "image/jpg" || $image['type'] == "image/jpeg" || $image['type'] == "image/pjpeg")
			{
				$simg = imagecreatefromjpeg($temp_loc.$file_name);
			} 
			elseif($image['type'] == "image/gif")
			{
				$simg = imagecreatefromgif($temp_loc.$file_name);
			}
			elseif($image['type'] == "image/png")
			{
				$simg = imagecreatefrompng($temp_loc.$file_name);
			}
				
			$currwidth = imagesx($simg);
			$currheight = imagesy($simg); 
			$newwidth = $currwidth; 
			$newheight = $currheight; 
			
			if($currheight > $theight || $currwidth > $twidth)
			{
				if($currheight > $currwidth)
				{  
					$zoom = $theight / $currheight;  
					$newheight = $theight;  
					$newwidth = $currwidth * $zoom; 
				}
				else
				{  
					$zoom = $twidth / $currwidth;
					$newwidth = $twidth;
					$newheight = $currheight * $zoom;
				}
			}
			
			$dimg = imagecreatetruecolor($newwidth, $newheight);
			$palsize = ImageColorsTotal($simg);
			
			for ($i = 0; $i < $palsize; $i++)
			{   
				$colors = ImageColorsForIndex($simg, $i);   
				ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);
			}
			
			imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);
			
			if($image['type'] == "image/jpg" || $image['type'] == "image/jpeg" || $image['type'] == "image/pjpeg")
			{
				imagejpeg($dimg, $location.$file_name);
			} 
			else if($image['type'] == "image/gif")
			{		
				imagegif($dimg, $location.$file_name);
			} 
			else if ($image['type'] == "image/png")
			{			
				imagepng($dimg, $location.$file_name);
			}
			if(file_exists($location.$file_name))
			{
				return true;
			}

			imagedestroy($dimg);
			imagedestroy($simg);
		}
	}
?>